/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.jiujiudevice.dao.JiuDeviceDao;
import com.boxin.ims.modules.jiujiudevice.dao.JiuSnDao;
import com.boxin.ims.modules.jiujiudevice.entity.JiuDevice;
import com.boxin.ims.modules.jiujiudevice.entity.JiuSn;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * sn配置Service
 * @author sxh
 * @version 2018-08-15
 */
@Component
@Transactional(readOnly = true)
public class JiuSnService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(JiuSnService.class);
	
	@Autowired
	private JiuSnDao jiuSnDao;
	@Autowired
	private JiuDeviceDao jiuDeviceDao;
	
	public JiuSn get(Long id) {
		return jiuSnDao.findOne(id);
	}
	public boolean existsBySn(String sn) {
		DetachedCriteria dc = jiuSnDao.createDetachedCriteria();
		dc.add(Restrictions.eq("sn", sn));
		return jiuSnDao.find(dc).size()>0;
	}
	public Page<JiuSn> find(Page<JiuSn> page, JiuSn jiuSn) {
		DetachedCriteria dc = jiuSnDao.createDetachedCriteria();
		if (jiuSn.getId()!=null && jiuSn.getId()>0){
			dc.add(Restrictions.eq("id", jiuSn.getId()));
		}
		if (StringUtils.isNotEmpty(jiuSn.getSn())){
			dc.add(Restrictions.like("sn", "%"+jiuSn.getSn()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return jiuSnDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(JiuSn jiuSn) {
		jiuSnDao.save(jiuSn);
	}
	@Transactional(readOnly = false)
	public boolean saveNx(JiuSn jiuSn) {
		if(!existsBySn(jiuSn.getSn())) {
			jiuSnDao.save(jiuSn);
			return true;
		}
		return false;
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		jiuSnDao.deleteById(id);
	}

	
	public List<JiuSn> find(JiuSn jiuSn) {
		DetachedCriteria dc = jiuSnDao.createDetachedCriteria();
		if (jiuSn.getId()!=null && jiuSn.getId()>0){
			dc.add(Restrictions.eq("id", jiuSn.getId()));
		}
		if (StringUtils.isNotEmpty(jiuSn.getSn())){
			dc.add(Restrictions.like("sn", "%"+jiuSn.getSn()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return jiuSnDao.find(dc);
	}
	/***
	 * 
	 * @function: <p>查询所有有效设备 </p >
	 * @return
	 */
	public List<JiuSn> findAll() {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuSnDao.createDetachedCriteria();
        //查询当前设备列表
		List<JiuDevice> jList = jiuDeviceDao.find();
		List<String> sList = Lists.newArrayList();
		if(null != jList && jList.size() >0 ){
			for (int i = 0; i < jList.size(); i++) {
				JiuDevice j = jList.get(i);
				sList.add(j.getSn());
			}
		}	
		if(null != sList && sList.size() >0) {
			dc.add(Restrictions.not(Restrictions.in("sn",sList)));
		}
		return jiuSnDao.find(dc);
	}
	/**
	 * 
	 * @function: <p> 根据sn码查询添备</p>
	 * @param sn
	 * @return
	 */
	public JiuSn getBySn(String sn) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuSnDao.createDetachedCriteria();
	    dc.add(Restrictions.eq("sn",sn));
		return jiuSnDao.find(dc) != null && jiuSnDao.find(dc).size() > 0 ? jiuSnDao.find(dc).get(0):null ;
	}
    /**
     * 	
     * @function: <p> 得到所有的sn</p>
     * @return
     */
	public List<JiuSn> findAllSn(){
		DetachedCriteria dc = jiuSnDao.createDetachedCriteria();
		return jiuSnDao.find(dc);
	}
} 
